package com.micangcaihang.micang.mvp.task.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.micangcaihang.common.base.fragment.BaseFragment;
import com.micangcaihang.common.utils.Properties;
import com.micangcaihang.common.utils.StringUtils;
import com.micangcaihang.common.utils.ToastUtils;
import com.micangcaihang.micang.R;
import com.micangcaihang.micang.databinding.FragmentTaskProductInfoFBinding;
import com.micangcaihang.micang.entity.FTaskProductInfoEntity;
import com.micangcaihang.micang.entity.PickerEntity;
import com.micangcaihang.micang.http.request.FTaskProductInfoRequest;
import com.micangcaihang.micang.mvp.task.contract.FTaskProductInfoContract;
import com.micangcaihang.micang.mvp.task.presenter.FTaskProductInfoPresenter;
import com.micangcaihang.micang.view.PickerViewHelper;

import java.util.List;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/07/24
 *     update : 2017/07/24
 *     desc   : 风控 产品信息
 *     version: 1.0
 * </pre>
 */

public class FTaskProductInfoFragment extends BaseFragment<FTaskProductInfoPresenter, FragmentTaskProductInfoFBinding> implements FTaskProductInfoContract.View {
    private String id;
    private FTaskProductInfoRequest mRequest;
    private FTaskProductInfoRequest mOldRequest;
    private int pickerType;
    private boolean isNetSuccess;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        id = getArguments().getString("id");
    }

    public static FTaskProductInfoFragment newInstance(String id) {
        Bundle args = new Bundle();
        FTaskProductInfoFragment fragment = new FTaskProductInfoFragment();
        args.putString("id", id);
        fragment.setArguments(args);
        return fragment;
    }

    /**
     * 获取资源文件布局
     *
     * @return 资源布局文件layout
     */
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_task_product_info_f;
    }

    @Override
    protected void initData() {
        super.initData();
        mRequest = new FTaskProductInfoRequest();
        mRequest.setId(id);
        mOldRequest = new FTaskProductInfoRequest();
        mOldRequest.setId(id);
    }

    @Override
    public void initPresenter() {
        super.initPresenter();
        mPresenter.setId(id);
    }

    /**
     * 初始化按钮监听
     */
    @Override
    protected void initListener() {
        mViewBinding.setClickHandler(new FTaskProductInfoFragment.ClickHandler());
    }

    /**
     * 返回下拉框信息
     *
     * @param list
     * @param type
     */
    @Override
    public void returnPickerViewData(List<PickerEntity> list, String type) {
        switch (pickerType) {
            case 1:
                PickerViewHelper.showOnePickerView(_mActivity, type, mRequest.getProduct_id(), list,
                        pickerEntity1 -> {
                            mRequest.setProduct_id(pickerEntity1.getSign_id());
                            mViewBinding.tvProductId.setText(pickerEntity1.getTitle());
                            if (!StringUtils.isEmpty(mRequest.getProduct_id()) && !StringUtils.isEmpty(mRequest.getLoan_type())) {
                                mPresenter.returnProductInfoCostData(mRequest.getProduct_id(), mRequest.getLoan_type());
                            }
                        });
                break;
            case 2:
                PickerViewHelper.showOnePickerView(_mActivity, type, mRequest.getLoan_type(), list,
                        pickerEntity1 -> {
                            mRequest.setLoan_type(pickerEntity1.getSign_id());
                            mViewBinding.tvLoanType.setText(pickerEntity1.getTitle());
                            if ("2".equals(mRequest.getLoan_type())) {
                                mViewBinding.expandableLayoutOneMortgage.expand();
                            } else {
                                mViewBinding.expandableLayoutOneMortgage.collapse();
                            }
                            if (!StringUtils.isEmpty(mRequest.getProduct_id()) && !StringUtils.isEmpty(mRequest.getLoan_type())) {
                                mPresenter.returnProductInfoCostData(mRequest.getProduct_id(), mRequest.getLoan_type());
                            }
                        });
                break;
            case 3:
                PickerViewHelper.showOnePickerView(_mActivity, type, mRequest.getLoan_nature(), list,
                        pickerEntity1 -> {
                            mRequest.setLoan_nature(pickerEntity1.getSign_id());
                            mViewBinding.tvLoanNature.setText(pickerEntity1.getTitle());
                        });
                break;
        }
    }

    /**
     * 返回产品信息
     *
     * @param entity
     */
    @Override
    public void returnProductInfoData(FTaskProductInfoEntity entity) {
        isNetSuccess = true;
        String[] excludsArray = new String[]{"choose_list", "id"};
        try {
            Properties.copyPropertiesExclude(entity, mRequest, excludsArray);
            Properties.copyPropertiesExclude(entity, mOldRequest, excludsArray);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //抵押类型
        if ("2".equals(mRequest.getLoan_type())) {
            mViewBinding.expandableLayoutOneMortgage.expand();
        } else {
            mViewBinding.expandableLayoutOneMortgage.collapse();
        }
        // //借款期限
        if (!StringUtils.isEmpty(entity.getLoan_term())) {
            mViewBinding.etLoanTerm.setText(entity.getLoan_term());
        }
        //服务费
        if (!StringUtils.isEmpty(entity.getCost_amount())) {
            mViewBinding.etCostAmount.setText(entity.getCost_amount());
        }
        //利率
        if (!StringUtils.isEmpty(entity.getLoan_rate())) {
            mViewBinding.etLoanRate.setText(entity.getLoan_rate());
        }
        //综合利率
        if (!StringUtils.isEmpty(entity.getTotal_rate())) {
            mViewBinding.etTotalRate.setText(entity.getTotal_rate());
        }
        //保费
        if (!StringUtils.isEmpty(entity.getInsurance_amount())) {
            mViewBinding.etInsuranceAmount.setText(entity.getInsurance_amount());
        }
        //一抵金额
        if (!StringUtils.isEmpty(entity.getOne_mortgage())) {
            mViewBinding.etOneMortgage.setText(entity.getOne_mortgage());
        }
        //一抵余额
        if (!StringUtils.isEmpty(entity.getOne_mortgage_balance())) {
            mViewBinding.etOneMortgageBalance.setText(entity.getOne_mortgage_balance());
        }
        //一抵银行机构
        if (!StringUtils.isEmpty(entity.getOne_mortgage_bank())) {
            mViewBinding.etOneMortgageBank.setText(entity.getOne_mortgage_bank());
        }
        if (entity.getChoose_list() != null) {
            FTaskProductInfoEntity.ChooseListBean chooseList = entity.getChoose_list();
            //产品类型
            if (!StringUtils.isEmpty(chooseList.getProduct_id())) {
                mViewBinding.tvProductId.setText(chooseList.getProduct_id());
            }
            //抵押类型
            if (!StringUtils.isEmpty(chooseList.getLoan_type())) {
                mViewBinding.tvLoanType.setText(chooseList.getLoan_type());
            }
            ////贷款性质
            if (!StringUtils.isEmpty(chooseList.getLoan_nature())) {
                mViewBinding.tvLoanNature.setText(chooseList.getLoan_nature());
            }
        }
        if ("2".equals(mOldRequest.getLoan_type())) {
            mOldRequest.setOne_mortgage(entity.getOne_mortgage());
            mOldRequest.setOne_mortgage_balance(entity.getOne_mortgage_balance());
            mOldRequest.setOne_mortgage_bank(entity.getOne_mortgage_bank());
        } else {
            mOldRequest.setOne_mortgage("");
            mOldRequest.setOne_mortgage_balance("");
            mOldRequest.setOne_mortgage_bank("");
            mOldRequest.setLoan_nature("");
        }
    }

    /**
     * 返回产品信息费用数据
     *
     * @param entity
     */
    @Override
    public void returnProductInfoCostData(FTaskProductInfoEntity entity) {
        // //借款期限
        if (!StringUtils.isEmpty(entity.getLoan_term())) {
            mViewBinding.etLoanTerm.setText(entity.getLoan_term());
        }
        //服务费
        if (!StringUtils.isEmpty(entity.getCost_amount())) {
            mViewBinding.etCostAmount.setText(entity.getCost_amount());
        }
        //利率
        if (!StringUtils.isEmpty(entity.getLoan_rate())) {
            mViewBinding.etLoanRate.setText(entity.getLoan_rate());
        }
        //综合利率
        if (!StringUtils.isEmpty(entity.getTotal_rate())) {
            mViewBinding.etTotalRate.setText(entity.getTotal_rate());
        }
        //保费
        if (!StringUtils.isEmpty(entity.getInsurance_amount())) {
            mViewBinding.etInsuranceAmount.setText(entity.getInsurance_amount());
        }
    }

    public class ClickHandler {
        public void onSave(View view) {
            saveData();
        }

        public void onShowProductIdView(View view) {
            pickerType = 1;
            mPresenter.loadPickerViewData("product_type", "产品类型");
        }

        public void onShowLoanTypeView(View view) {
            pickerType = 2;
            mPresenter.loadPickerViewData("loan_type", "抵押类型");
        }

        public void onShowLoanNatureView(View view) {
            pickerType = 3;
            mPresenter.loadPickerViewData("loan_nature", "贷款性质");
        }
    }

    /**
     * 开始保存请求数据
     */
    public void saveData() {
        setSaveData();
        if (mRequest.toString().equals(mOldRequest.toString())) {
            ToastUtils.showLong("您没有更改任何信息，无需保存");
            return;
        }
        isNetSuccess = false;
        mPresenter.saveProductInfo(mRequest);
    }

    /**
     * 判断是否保存过数据 没有的话点击保存去保存
     *
     * @return 是否保存当前页面数据
     */
    public boolean isSaveData() {
        //如果请求错误则返回true
        if (!isNetSuccess) {
            return true;
        }
        setSaveData();
        if (mRequest.toString().equals(mOldRequest.toString())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * s设置请求数据
     */
    private void setSaveData() {
        mRequest.setLoan_term(mViewBinding.etLoanTerm.getText().toString().trim());
        mRequest.setCost_amount(mViewBinding.etCostAmount.getText().toString().trim());
        mRequest.setLoan_rate(mViewBinding.etLoanRate.getText().toString().trim());
        mRequest.setTotal_rate(mViewBinding.etTotalRate.getText().toString().trim());
        mRequest.setInsurance_amount(mViewBinding.etInsuranceAmount.getText().toString().trim());
        if ("2".equals(mRequest.getLoan_type())) {
            mRequest.setOne_mortgage(mViewBinding.etOneMortgage.getText().toString().trim());
            mRequest.setOne_mortgage_balance(mViewBinding.etOneMortgageBalance.getText().toString().trim());
            mRequest.setOne_mortgage_bank(mViewBinding.etOneMortgageBank.getText().toString().trim());
        } else {
            mRequest.setOne_mortgage("");
            mRequest.setOne_mortgage_balance("");
            mRequest.setOne_mortgage_bank("");
            mRequest.setLoan_nature("");
        }
    }
}
